﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using control.Biz;


namespace control
{
    public partial class QLKhachHang : Form
    {
        List<BizKhachHang> khachHang = new List<BizKhachHang>();
        List<BizKhachHang> khachHangToDelete = new List<BizKhachHang>();
        public QLKhachHang()
        {
            InitializeComponent();
        }

        private void QLKhachHang_Load(object sender, EventArgs e)
        {
            khachHang = BizKhachHang.GetAll();
            dataGridView_DSKH.AutoGenerateColumns = false;
            dataGridView_DSKH.DataSource = khachHang;


        }


        private void label_MaKH_Click(object sender, EventArgs e)
        {

        }


        private void groupBox_KhachHang_Enter(object sender, EventArgs e)
        {

        }

        private void button_ThemKH_Click(object sender, EventArgs e)
        {
            BizKhachHang kh = new BizKhachHang(-1, textBox_MaKH.Text, textBox_TenDN.Text, textBox_MatKhau.Text, textBox_TenKH.Text, textBox_DiaChi.Text, textBox_Email.Text, int.Parse(textBox_SDT.Text), new BizQuyen("Q002", "Member"));
            khachHang.Add(kh);

            dataGridView_DSKH.DataSource = null;
            dataGridView_DSKH.DataSource = khachHang;

        }

        private void textBox_TenDN_TextChanged(object sender, EventArgs e)
        {

        }

        private void button_XoaKH_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView_DSKH.SelectedRows)
            {
                BizKhachHang kh = (BizKhachHang)row.DataBoundItem;// chon dong xoa

                khachHang.Remove(kh);
                khachHangToDelete.Add(kh);

                dataGridView_DSKH.DataSource = null;
                dataGridView_DSKH.DataSource = khachHang;


            }

            
        }

        private void button_SuaKH_Click(object sender, EventArgs e)
        {
            if (dataGridView_DSKH.SelectedRows.Count > 0)
            {
                BizKhachHang kh = (BizKhachHang)dataGridView_DSKH.SelectedRows[0].DataBoundItem;
                kh.MaKhachHang = textBox_MaKH.Text;
                kh.TenDN = textBox_TenDN.Text;
                kh.Matkhau = textBox_MatKhau.Text;
                kh.TenKH = textBox_TenKH.Text;
                kh.Diachi = textBox_DiaChi.Text;
                kh.Email = textBox_Email.Text;
                kh.SDT = Convert.ToInt32(textBox_SDT.Text);

                
                kh.update = true;
                dataGridView_DSKH.DataSource = null;
                dataGridView_DSKH.DataSource = khachHang;
            }


        }

        private void button_LuuKH_Click(object sender, EventArgs e)
        {
            foreach (BizKhachHang kh in khachHang)
            {
                try
                {
                    if (kh.IDKhachHang == -1)
                        kh.add();
                    if (kh.update)
                        kh.updated();

                }
                catch (Exception ex)
                {
                    //string s = String.Format("Bị lỗi ở khách hàng ({0}-{1})",
                    // kh.MaKhachHang,kh.TenKhachHang);
                    MessageBox.Show(ex.Message, "Lỗi");

                    return;
                }
            }
            foreach (BizKhachHang kh in khachHangToDelete)
                kh.delete();


        }

        private void button_DongKH_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView_DSKH_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
